﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.QualityTools.Testing.Fakes;
using System.Web.Security;

namespace T2.WebSecurity.Test
{
    [TestClass]
    public class MembershipProviderTest
    {
        SimpleMembershipProvider provider = new SimpleMembershipProvider();

        [TestMethod]
        public void ChangePassword()
        {
            string username = "trooks";
            string oldPassword = "trooks123";
            string newPassword = "duminda123";
            bool status = provider.ChangePassword(username, oldPassword, newPassword);
            Assert.IsTrue(status);
        }


        [TestMethod]
        public void ConfirmAccount()
        {
            string confirmationString = "RW12FoemuIZcpxUj5hF3fQ2";
            

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };

                bool status = WebMatrix.WebData.WebSecurity.ConfirmAccount(confirmationString);

                Assert.IsTrue(status);
            }
            
            
        }

        [TestMethod]
        public void CreateUserAndAccount()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;

            string username = "john";
            string pass = "john123";
            System.Collections.Generic.Dictionary<string, object> dict = 
                new System.Collections.Generic.Dictionary<string, object>
            { 
                { "FirstName" , "John" },
                { "LastName" , "Sena" },
                { "Email" , "dumindapium@gmail.com" },
                { "Address" , "address" },
                { "City" , "city"},
                { "Region" , "region"},
                { "PostalCode" , "789"},
                { "OfficePhone" , "098098"},
                { "CellPhone" , "986987"},
                { "Title" , "Mr."},
                { "PasswordQuestion" , "1"},
                { "PasswordAnswer" , "none"},
                { "IsApproved" , "false"}
            };
            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);
                string ConfirmationToken = provider.CreateUserAndAccount(username, pass,true, dict);

                Assert.IsNotNull(ConfirmationToken);
            }
        }

        [TestMethod]
        public void DeleteAccount()
        {
            string username = "john";
            bool status = provider.DeleteAccount(username);
            Assert.IsTrue(status);
        }

        [TestMethod]
        public void GeneratePasswordResetToken()
        {
            string token = provider.GeneratePasswordResetToken("trooks");
            Assert.IsNotNull(token);
        }

        [TestMethod]
        public void ResetPassword()
        {
            string status = provider.ResetPassword("trooks", "None");
        }

        [TestMethod]
        public void ValidateUser()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";

            bool valid = provider.ValidateUser(testUserName, testPassword);
            Assert.IsTrue(valid);
        }

        [TestMethod]
        public void GetUserByName()
        {
            string testUserName = "trooks";
            MembershipUser user = provider.GetUser(testUserName, false);
            Assert.AreEqual(user.UserName, testUserName);
        }

    }
}
